﻿using Sliver.Roadnet.OpenDrive;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using UnityEditor.Experimental.GraphView;
using UnityEngine;

namespace Sliver.Scene.BuildOpenDrive
{
    public class SceneManager : MonoBehaviour
    {
        public GameObject Junction;
        public GameObject RoadPiece;

        public OpenDriveRoadnet Roadnet;

        protected void Start()
        {
            XmlDocument document = new XmlDocument();
            TextAsset textAsset = Resources.Load("Source/base_map", typeof(TextAsset)) as TextAsset;
            // TextAsset textAsset = Resources.Load("Source/rdb2", typeof(TextAsset)) as TextAsset;
            if (textAsset != null) document.Load(new StringReader(textAsset.text));
            // var root = document.SelectSingleNode("OpenDRIVE");
            // Debug.Log(textAsset.text);

            Roadnet = new OpenDriveRoadnet();
            Roadnet.Init(document);

        }

        protected const float BaseX = 1.163f;
        protected const float BaseY = 3.571f;
        protected static Vector3 Offset = new Vector3(BaseX, 0, BaseY);


        protected void CreateRoad(XmlNode root)
        {
        }

        Vector3 offset = Vector3.zero;
        int pointCount = 0;

        protected void PrepareJunction(XmlNode root)
        {
            foreach (XmlNode node in root.ChildNodes)
            {
                switch (node.Name)
                {
                    case "outline":
                        Vector3 v = Vector3.zero;
                        foreach (XmlNode point in node.ChildNodes)
                        {
                            float x = float.Parse(point.Attributes["x"].Value);
                            float y = float.Parse(point.Attributes["y"].Value);
                            float z = float.Parse(point.Attributes["z"].Value);

                            offset += new Vector3(x, z, y);
                            pointCount++;
                        }
                        break;
                }
            }

            pointCount++;
        }

        protected void CreateJunction(XmlNode root)
        {

            Debug.Log(root.OuterXml);
            foreach (XmlNode node in root.ChildNodes)
            {
                switch (node.Name)
                {
                    case "outline":
                        Vector3 v = Vector3.zero;
                        foreach (XmlNode point in node.ChildNodes)
                        {
                            float x = float.Parse(point.Attributes["x"].Value);
                            float y = float.Parse(point.Attributes["y"].Value);
                            float z = float.Parse(point.Attributes["z"].Value);
                            v += new Vector3(x, z, y) - offset;
                        }

                        v /= node.ChildNodes.Count;

                        Instantiate(Junction, v, Quaternion.identity);
                        break;
                }
            }
        }
    }
}